<?php
// +----------------------------------------------------------------------
// | Ntalker插件安装程序 UCHome                                                             
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.ntalker.com All rights reserved.      
// +----------------------------------------------------------------------
// | Author: lsh
// | Latest Update:    2010-10-18                                 
// +----------------------------------------------------------------------
    @session_start();
    error_reporting(7);
    @set_time_limit(1000);
    if(!$_SESSION['installid']){
         $ins_start = true;
         $_SESSION['installid'] = date('Ymdhms',time());
    }else{
         $ins_start = false;
    }
    if(isset($_GET['newversion'])){
         $_SESSION['topversion'] = trim($_GET['newversion']);
         $_SESSION['issupport'] = trim($_GET['newissupport']);
    } 
    
    imshow_header();

    define('IN_UCHOME', TRUE);
    define('IN_NTALKER', TRUE);
    define('SELF_FILE', $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
    define('INSTALLROOT', substr(dirname(__FILE__),0,-7));
    define('SITEROOT', $_SERVER['DOCUMENT_ROOT'].'/');
    
    $recordurl = 'http://www.ntalker.com/autoopen.php';

    //获取安装包中网站信息
    if( file_exists(INSTALLROOT.'./configs.php')){
         $cfgarr  = include(INSTALLROOT.'./configs.php');
         $im_siteid    = $cfgarr[0];
         $im_sitekey   = $cfgarr[2];
         $im_domainurl = 'http://'.$cfgarr[3];
         $domainurl    = $cfgarr[3];
    }else{
         $im_siteid    = "ntalkerdemo";
         $im_sitekey   = "";
         $im_domainurl = "";
         $domainurl    = "";
    }
    //开通服务验证key
    $insauthkey = md5($im_siteid.'_'.$im_sitekey);
    $errormsg = $versionmsg = $installSQL = '';
    $pluginCfg = $insdb = $insfilelist = $clsreg = $_config = $crossdomain = array();
    
    //发送安装程序开始
    if($ins_start && !$sid){
         $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"start"}';
         interact($jsonparm);
    }
    
    //1-检查安装包运行所需文件
    $insfiles = array('./ntalker_config.php','./ntalker.inc.php');
    foreach($insfiles AS $insfile){
         if(file_exists(INSTALLROOT.$insfile)){
              @include_once (INSTALLROOT.$insfile);
         }else{
              $errormsg = $errormsg ? $errormsg.$insfile.' <br />' : $insfile.' <br />';
              $errordesc = 'install_run_needfile_absence';
         }
    }
    if($errormsg){
         $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
         interact($jsonparm);
         $dealmsg ='步骤1：删除服务器上的ntalker安装文件夹。<br />步骤2：登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）<br />
                    步骤3：解压安装包并把ntalker文件夹上传到UCHome目录，且确认所有文件上传完毕。<br />步骤4：在浏览器里重新运行安装程序。';
         imshow_msg('问题：安装包不完整，缺少安装文件：',$errormsg,$dealmsg,false,false);
    }
    
     //2-检查论坛目录 
     if(!defined('Uchome_ROOT')){
         // 首先检查安装目录是否为论坛目录
         if(file_exists(INSTALLROOT.'./config.php')){
              define('Uchome_ROOT', INSTALLROOT);
              //如果安装目录即为论坛目录，则在data目录下建立ntalker目录，保存安装后的ntalker文件
              if(!is_dir(Uchome_ROOT.'./data/ntalker')) {
                  @mkdir(Uchome_ROOT.'./data/ntalker', 0777);
              }
              define('NTALKER_DATA_ROOT',Uchome_ROOT.'./data/ntalker/');
         }else{
              $inslenth = check_bbsdir();
              if($inslenth==-1){
                   //检查到网站目录还没有找到论坛数据库配置文件，则提示错误
                   $errormsg = '步骤1：请将Ntalker安装文件夹移动到uchome目录下，重新安装。<br />步骤2：确认网站软件类型为UCHome，如果不符，登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）';
                   $errordesc = 'site_root_invalid';
                   
                   $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
                   interact($jsonparm);
                   imshow_msg('问题：无法找到安装所需的UCHome文件。','',$errormsg);
              }
         }
     } 
     //3-加载论坛配置文件
     if(file_exists(Uchome_ROOT.'./config.php')){
          if(!file_exists(Uchome_ROOT.'./ver.php')){
               @include(Uchome_ROOT.'./common.php');
          }else{
               @include(Uchome_ROOT.'./config.php'); 
               @include(Uchome_ROOT.'./ver.php');
               @include(Uchome_ROOT.'./source/function_common.php');
          }
          define('INSCHARSET',strtolower($_SC['charset']));             
     }
     global $_config,$clsreg,$crossdomain,$installSQL,$insfilelist,$pluginCfg,$insdb,$insauthkey;
     //2010-12-07 增加sid参数，避免session无法保存的网站，重复发送start参数 
     InitGP(array('error','action','type','instemplate','result','sid','tpl'));
     
     //向服务器获取信息
     if(!$_SESSION['topversion'] && $ins_start && !$sid){
          $jsonparm = '{siteid:"'.$im_siteid.'",action:"validation",domain:"'.$im_domainurl.'",
                        sitesoftwaretype:"uchome",sitesoftwareversion:"'.X_VER.'",ntalkerversion:"'.$pluginCfg['version'][X_VER].'"}';
          $callback = 'function(){if(typeof(topversion)!="undefined"){window.location.href="'.SELF_FILE.'?sid=1&newversion=" + topversion + "&newissupport=" + issupport + "&rand='.rand(1,10).'";}}';
          interact($jsonparm,$callback);
     }

    //4-获取网站域名和字符集
    if(!defined("INSSITEURL")){    
         $install_lenth = strlen(str_replace('\\','/',INSTALLROOT))-strlen(str_replace('\\','/',Uchome_ROOT));
         $insbbsurl = htmlspecialchars( 'http://'.$_SERVER['HTTP_HOST'].substr(SELF_FILE, 0, strrpos(SELF_FILE, '/')-$install_lenth-7));
         define('INSSITEURL',$insbbsurl);
    }
    //安装目录对于论坛目录的相对路径
    define('SHOW_INS_ROOT',str_replace(str_replace('\\','/',Uchome_ROOT),'./',str_replace('\\','/',INSTALLROOT)));
    $sub_dir = str_replace(str_replace('\\','/',Uchome_ROOT),'',str_replace('\\','/',INSTALLROOT));
    $sub_dir = substr(INSSITEURL,-1,1)=='/' ? $sub_dir : '/'.$sub_dir;
    $sub_dir = substr($sub_dir,-1,1)=='/' ? $sub_dir : $sub_dir.'/';
    define('SHOW_INS_SITEURL',$sub_dir);

    //5-判断是否安装锁定
    if(inschecklock()){
          $errormsg = '请登录网站服务器，删除UCHome目录下 '.SHOW_INS_ROOT.'install.lock 文件，再点击【重新检查】。';
          $errordesc = 'install_locked';
          $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
          interact($jsonparm);
          imshow_msg('问题：检测到安装保护文件。','',$errormsg,false,true);
    }
    
    //6-判断当前网站域名和安装包指定域名是否一致
    //if(rtrim(INSSITEURL,'/') != rtrim($im_domainurl,'/')){
    if(strrpos(rtrim(INSSITEURL,'/'),rtrim($im_domainurl,'/'))===false && strrpos(rtrim($im_domainurl,'/'),rtrim(INSSITEURL,'/'))===false){
         $addmsg = '安装包绑定网站地址：'.rtrim($im_domainurl,'/').' <br />
                    当前网站地址：'.rtrim(INSSITEURL,'/').' 不一致!';
         $errormsg = '1、如果两个地址指向同一网站，请在<a href="'.$im_domainurl.'">'.$im_domainurl.'</a>地址下运行安装程序。<br />
                      2、如果两个地址指向的不是同一网站，请登录登陆站长管理中心为当前网站申请新Ntalker，下载相应的安装包并安装（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）';
         $errordesc = 'install_siteurl_invalid';
         $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
         interact($jsonparm);
         imshow_msg('问题：安装包绑定的网站地址与当前网站地址不符',$addmsg,$errormsg);
    }

    //7-判断是否从服务器获取了最新版本，并比对是否需要强制升级
    if(isset($_SESSION['topversion'])){
         //确认服务器版本等于当前版本
         strcmp($_SESSION['topversion'],$pluginCfg['version'][X_VER])==0 && define('SERVER_VERSION',true);
         if(strcmp($_SESSION['topversion'],$pluginCfg['version'][X_VER])>0){
              //重大版本更新，强制重新下载安装包
              if(strncmp($_SESSION['topversion'],$pluginCfg['version'][X_VER],3)>0){
                   $addmsg = '当前安装包版本：'.$pluginCfg['version'][X_VER].'<br />最新安装包版本：'.$_SESSION['topversion'];
                   $errormsg = '登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）';
                   $errordesc = 'install_version_low';
                   $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
                   interact($jsonparm);
                   imshow_msg('问题：当前Ntalker安装包版本过低，请下载最新版本安装。',$addmsg,$errormsg);
              }else{
                   $addvermsg = '当前安装包版本：'.$pluginCfg['version'][X_VER].'<br />最新安装包版本：'.$_SESSION['topversion'];
                   $versionmsg = '方法1：登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）<br />
                                  方法2：确认安装当前版本，点击【继续安装】。';
                   $versiondesc = 'install_version_low2';
                   $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$versiondesc.'"}';
                   interact($jsonparm);
                   imshow_msg('问题：安装程序检测到有最新版本。',$addvermsg,$versionmsg,true,false);
              }
         }          
    }
    
    //8-判断是否存在已安装版本信息，并与当前版本比对
    if(!defined("INSTALL_VERSION_NEW")){
         if(defined('SERVER_VERSION')){
              define('INSTALL_VERSION_NEW', 'equal');
         }else{
              if(defined("NTALKER_DATA_ROOT")){
                   $vfilename = NTALKER_DATA_ROOT.'./ntalkerdata/ntalker_version.php';
              }else{
                   $vfilename = INSTALLROOT.'./ntalkerdata/ntalker_version.php';
              }
              if(file_exists($vfilename)){
                   include ($vfilename);
                   if($ntalker_ver[X_VER] > $pluginCfg['version'][X_VER]){
                        define('INSTALL_VERSION_NEW', 'lower');
                   }elseif($ntalker_ver[X_VER] == $pluginCfg['version'][X_VER]){
                        define('INSTALL_VERSION_NEW', 'equal');
                   }else{
                        define('INSTALL_VERSION_NEW', 'higher');
                   }
              }else{
                   define('INSTALL_VERSION_NEW', 'higher');
              }
         }
         if(INSTALL_VERSION_NEW=='lower'){
              $addvermsg = '当前安装包版本：'.$pluginCfg['version'][X_VER].'<br />已安装版本：'.$ntalker_ver[X_VER];
              $versionmsg = '方法1：登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）<br />
                             方法2：确认安装当前版本，点击【继续安装】。';
              $versiondesc = 'install_current_version_low';
              $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$versiondesc.'"}';
              interact($jsonparm);
              imshow_msg('问题：当前安装包版本比已安装版本旧。',$addvermsg,$versionmsg,true,false);
         }        
    }    
    

    //9-判断是否有版本配置文件
    $configfilename = './install/'.INSCHARSET.'/config_'.X_VER.'.php';
    if(inscheckversion($configfilename)){
          $insfilelist = @include(INSTALLROOT.$configfilename);
    }else{
         if(defined('SERVER_VERSION')){
              if($_SESSION['issupport']){
                   $dealmsg ='步骤1：删除服务器上的ntalker安装文件夹。<br />步骤2：登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）<br />
                              步骤3：解压安装包并把ntalker文件夹上传到UCHome目录，且确认所有文件上传完毕。<br />步骤4：在浏览器里重新运行安装程序。';
                   $errordesc = 'install_config_file_absence';
                   $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
                   interact($jsonparm);
                   imshow_msg('问题：缺少配置文件：',$configfilename,$dealmsg);
              }else{
                   $errormsg = '请等待发布新版安装包，或联系安装支持ntalker@ntalker.com';
                   $errordesc = 'install_unsupported';
                   $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
                   interact($jsonparm);
                   imshow_msg('问题：Ntalker暂未支持该版本UCHome。','',$errormsg);
              }
         }elseif(!isset($_SESSION['topversion'])){
              $errormsg = '请重新上传安装文件或联系安装支持了解Ntalker安装包是否支持该版本UCHome。';
              $errordesc = 'install_config_file_absence2';
              $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
              interact($jsonparm);
              imshow_msg('问题：缺少配置文件：',$configfilename,$errormsg);
         } 
    }

    //10-判断是否有当前版本接口文件等
    $ins_files = array('imxml.php','imconfig.inc.php');
    foreach($ins_files as $ins_file){
         if(!file_exists(INSTALLROOT.'./install/'.INSCHARSET.'/'.X_VER.'_'.$ins_file)){
              if(!file_exists(INSTALLROOT.'./install/'.INSCHARSET.'/'.$ins_file)){
                   $errormsg = $errormsg ? $errormsg.$ins_file.' <br />' : $ins_file.' <br />';
                   $errordesc = 'install_config_needfile_absence';
              }
         }
    }
    if($errormsg){
         $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"precheck",desc:"'.$errordesc.'"}';
         interact($jsonparm);
         $dealmsg ='步骤1：删除服务器上的ntalker安装文件夹。<br />步骤2：登陆站长管理中心下载安装包（进入站长管理中心：<a href="http://site.ntalker.com">site.ntalker.com</a>）<br />
                    步骤3：解压安装包并把ntalker文件夹上传到UCHome目录，且确认所有文件上传完毕。<br />步骤4：在浏览器里重新运行安装程序。';
         imshow_msg('问题：缺少接口文件：',$errormsg,$dealmsg);
    }

    //开通服务的参数
    $isfulltoolbar = $isallowthread = 0;
    if(X_VER=="2.0"){
         $isfulltoolbar = 1;
    }
    
    //接口文件地址
    $interface = defined("NTALKER_DATA_ROOT") ? INSSITEURL.'data/ntalker/imxml.php' : INSSITEURL.SHOW_INS_SITEURL.'imxml.php';
    //配置文件地址
    $imconfigfile = defined("NTALKER_DATA_ROOT") ? Uchome_ROOT.'data/ntalker/imconfig.inc.php' : INSTALLROOT.'imconfig.inc.php';

    switch($action){
        case "checkperm":  //安装
    ?>
        <div class="zc_menu">
            <ul><li class="m1"></li><li class="m22"></li><li class="m33"></li></ul>
        </div>
        <div class="cont">
        <div class="c_bj1"></div>
        <div class="c_bj2" style="overflow:auto;">
        <table width="76%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr class="f_blod">
              <td width="58%" height="30">项目</td>
              <td width="20%">所需配置</td>
              <td width="22%">当前配置</td>
            </tr>
            <tr>
              <td height="30">PHP版本</td>
              <td>PHP4.0以上</td>
              <td><img src="images/ok.gif" width="16" height="14" /><?php echo(PHP_VERSION); ?></td>
            </tr>
            <tr>
              <td height="30">网站软件版本</td>
              <td>1.5-2.0</td>
              <td><img src="images/ok.gif" width="16" height="14" /><?php echo(X_VER); ?></td>
            </tr>
          </table>
          <table width="76%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr class="f_blod">
              <td height="30">目录 文件权限检查</td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr class="f_blod">
              <td width="58%" height="30">目录文件</td>
              <td width="20%">所需状态</td>
              <td width="22%">当前状态</td>
            </tr>
        <?php
           if(checkInstall() && $type!='reinstall') {//已安装过此插件
                exit('<script>window.location.href=\''.SELF_FILE.'?sid=1&action=installed\';</script>');
           }else{
                $tplid = $tpl ? implode(',', $tpl) : '';
                !$type && $type = 'install';
                $state = inscheckperm($type,$tplid); 
                if($state) {
                     $retry = 'disabled="disabled" style="display:none"';
                     $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"install:checkperm:finish"}';
                     interact($jsonparm);
                }else{
                     $successd = 'disabled="disabled" style="display:none"';
                 }
           }
        ?>
        </table>
        </div>
        <div class="c_bj3"></div>        
        <div class="bu">
            <INPUT TYPE="button"  class="but_"  VALUE="重新检查" <?php echo $retry;?> onclick="window.location.href='<?php echo(SELF_FILE);?>?sid=1&action=checkperm&type=<?php echo $type ?>'" />
            <INPUT TYPE="button"  class="but_"  VALUE="下一步" <?php echo($successd);?> onclick="window.location.href='<?php echo(SELF_FILE);?>?sid=1&action=<?php echo $type ?>&instemplate=<?php echo $tplid ?>'" />
        </div></div>
<script language="javascript" type="text/javascript">
 var checkstat = '<?php echo $retry;?>';
 if(!checkstat) {
       var errorperm = document.getElementsByName("checkperm");
       var errorvalue = '';
       for(var i=0; i<errorperm.length; i++)
        {
             errorvalue = errorvalue ? errorvalue + ":" + errorperm[i].value : errorperm[i].value;
        }
       createScript("<?php echo($recordurl);?>",{action: "record",record: "install:checkperm",sessionid: "<?php echo ($_SESSION['installid']);?>",siteid: "<?php echo ($im_siteid);?>",desc:errorvalue}); 
 }      
</script>
	<?php	break;
	    case "install":  //安装
    ?>
        <div class="zc_menu">
            <ul><li class="m11"></li><li class="m2"></li><li class="m33"></li></ul>
        </div>
        <div class="cont">
        <div class="c_bj1"></div>
        <div class="c_bj2" style="overflow:auto;">
	    <?php
           $tplid = $instemplate ? $instemplate : '';
           $returnvalue = Install($tplid, 0, $im_siteid, $im_enablesitekey, $im_sitekey);
           if($returnvalue) {
                $retry = 'disabled="disabled" style="display:none"';
                $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"install:active",desc:encodeURIComponent("interface:'.$interface.',isfulltoolbar:1,build:'.$pluginCfg['release'].',isthread:1")}';
                interact($jsonparm);
           }else{
                $successd = 'disabled="disabled" style="display:none"';
           }
        ?>
        </div>
        <div class="c_bj3"></div>        
		<div class="bu">
            <INPUT TYPE="button"  class="but_"  VALUE="退  出" <?php echo $retry;?> onclick="window.location.href='<?php echo(INSSITEURL);?>'" />
		    <INPUT TYPE="button"  class="but_"  VALUE="下一步" <?php echo($successd);?> onclick=activentalker() />
		</div></div>
<script language="javascript" type="text/javascript">
 var installkstat = '<?php echo $retry;?>';
 if(!installkstat) {
       var installmsg = document.getElementsByName("installmsg");
       var installvalue = '';
       for(var i=0; i<installmsg.length; i++)
        {
             installvalue = installvalue ? installvalue + ":" + installmsg[i].value : installmsg[i].value;
        }
       createScript("<?php echo($recordurl);?>",{action: "record",record: "install:install",sessionid: "<?php echo ($_SESSION['installid']);?>",siteid: "<?php echo ($im_siteid);?>",desc:installvalue}); 
 }      
</script>
    <?php    break;
        case "reinstall":  //重新安装
    ?>
        <div class="zc_menu">
            <ul><li class="m11"></li><li class="m2"></li><li class="m33"></li></ul>
        </div>
        <div class="cont">
        <div class="c_bj1"></div>
        <div class="c_bj2" style="overflow:auto;">
        <?php
           $tplid = $tpl ? implode(',', $tpl) : ''; 
           $returnvalue = Install($tplid, 1, $im_siteid, $im_enablesitekey, $im_sitekey);
           if($returnvalue) {
                $retry = 'disabled="disabled" style="display:none"';
                $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"reinstall:active",desc:encodeURIComponent("interface:'.$interface.',isfulltoolbar:1,build:'.$pluginCfg['release'].',isthread:1")}';
                interact($jsonparm);
           }else{
                $successd = 'disabled="disabled" style="display:none"';
           }
        ?>
        </div>
        <div class="c_bj3"></div>        
        <div class="bu">
            <INPUT TYPE="button"  class="but_"  VALUE="退  出" <?php echo $retry;?> onclick="window.location.href='<?php echo(INSSITEURL);?>'" />
            <INPUT TYPE="button"  class="but_"  VALUE="下一步" <?php echo($successd);?> onclick=activentalker() />
        </div></div>
<script language="javascript" type="text/javascript">
 var reinstallkstat = '<?php echo $retry;?>';
 if(!reinstallkstat) {
       var reinstallmsg = document.getElementsByName("reinstallmsg");
       var reinstallvalue = '';
       for(var i=0; i<reinstallmsg.length; i++)
        {
             reinstallvalue = reinstallvalue ? reinstallvalue + ":" + reinstallmsg[i].value : reinstallmsg[i].value;
        }
       createScript("<?php echo($recordurl);?>",{action: "record",record: "reinstall:install",sessionid: "<?php echo ($_SESSION['installid']);?>",siteid: "<?php echo ($im_siteid);?>",desc:reinstallvalue}); 
 }      
</script>
	<?php  break;
	    case "installed":  //已经安装过
    ?>
        <div class="topnav" >已经安装过Ntalker</div>
        <div class="cont">
        <div class="c_bj1"></div>
        <div class="c_bj2">
		<?php
			if(!checkInstall()){
                exit('<script language="javascript">window.location.href=\''.SELF_FILE.'?sid=1&action=mode\';</script>');
            }else{
                 $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"install:mode:installed"}';
                 interact($jsonparm); 
                 message(false, "您已安装过Ntalker插件，请选择【卸载】或者【重新安装】！<br />",true);
            }
		?>			
		</div>
        <div class="c_bj3"></div>
        <div class="bu2">
            <INPUT type="button"  class="but_"  name="btnclear" value="重新安装" onclick="window.location.href='<?php echo(SELF_FILE);?>?sid=1&type=reinstall';" />
			<INPUT type="button"  class="but_"  name="btnclear" value="卸  载" onclick="window.location.href='<?php echo(INSSITEURL.SHOW_INS_SITEURL.'uninstall.php');?>';" />
		</div></div>
    <?php  break;
        case "insfinish":  //安装完成
    ?>
        <div class="zc_menu">
            <ul><li class="m11"></li><li class="m22"></li> <li class="m3"></li></ul>
        </div>
        <div class="cont">
        <div class="c_bj1"></div>
        <div class="c_bj2" id="info">
        <?php  
             if($result=='0000'){
                  echo '<span class="f14 f12_green f_blod"><img src="images/ok.gif" width="16" height="14" />恭喜，您的网站已经成功安装并开通Ntalker服务！</span><br /><br /><br />'
                        .'<span class="f14 f_0 f_blod">即刻体验更好的安装服务，您可以：</span><br /><br />'
                        .'<span class="f14 f_0 f_blod">◆ 进入网站后台，清理缓存 <a href="'.INSSITEURL.'admincp.php" target=”_blank” class="f_blue">点击这里清理缓存</a><br />'
                        .'◆ 进入网站管理中心，为网站定制个性化的Ntalker服务 <a href="http://site.ntalker.com" target=”_blank” class="f_blue">进入站长管理中心</a></span>';
                  open_ntalker($imconfigfile);
             }else{
                  echo '<span class="f14 f_blod font12_red">开通失败，请联系Ntalker技术支持（邮箱：<a href="mailto:ntalker@ntalker.com"><span class="f_blue">ntalker@ntalker.com</span></a>，电话：010-64419266-8011）<br /><br />
                        <span class="f14 f_0 ">请注明UCHome地址，UCHome类型，版本及联系方式。（例如：www.test.com，discuz7.2 gbk，联系方式：admin@test.com） </span > <br / ><br / ><span class = "f14 f_0 f_blod" >开通失败原因：';
                  if($result=='9999'){
                       echo '无法连接Ntalker服务器，请检查网络连接.</span>';
                  }else{
                       $errarray = array('0010'=>'查询参数不完整.',
                                         '0011'=>'接口文件路径错误或无法访问.',
                                         '0100'=>'验证SITEID失败.',
                                         '0101'=>'Authkey验证失败.',
                                         '0110'=>'此安装包非当前网站对应安装包.',
                                         '0111'=>'更新网站信息失败.');
                       if($errarray[$result]){
                            echo $errarray[$result].'</span>';
                       }else{
                            echo '网络错误.</span>';
                       }
                  }
             }
	         $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"install:finish"}';
	         interact($jsonparm);
	         if(defined("NTALKER_DATA_ROOT")){
	              @unlink(Uchome_ROOT.'./install/index.php');
	              @unlink(Uchome_ROOT.'./uninstall.php');
	         }else{
	              @touch(INSTALLROOT.'install.lock');
	         }
        ?>            
        </div>
        <div class="c_bj3"></div>
        <div class="bu">
            <INPUT type="button"  class="but_"  value="完  成" onclick="window.location.href='<?php echo(INSSITEURL);?>';" />
        </div></div>
	<?php	break;
	    default : //显示安装协议
    ?>
        <div class="topnav" >Ntalker社区聊天插件用户使用协议</div>
	    <div class="cont">
        <div class="c_bj1"></div>
        <div class="c_bj2" style="overflow:auto;">
        <?php
            if(checkInstall() && $type!='reinstall') {//已安装过此插件
                exit('<script>window.location.href=\''.SELF_FILE.'?sid=1&action=installed\';</script>');
            }
            echo $protocol;
            $jsonparm = '{sessionid:"'.$_SESSION['installid'].'",siteid:"'.$im_siteid.'",action:"record",record:"agreement"}';
            interact($jsonparm);
        ?>            
        </div>
        <div class="c_bj3"></div>
        <div class="bu">
            <INPUT type="button"  class="but_"  name="btnclear" value="我同意" onclick="window.location.href='<?php echo(SELF_FILE);?>?sid=1&action=checkperm&type=<?php echo($type);?>';" />
        </div></div>
<?php
		break;
}
?>
<script language="javascript">
    function activentalker(){
         var serverurl = '<?php echo($recordurl);?>';
         var activityparm = {siteid:"<?php echo($im_siteid);?>",type:"installer",authkey:"<?php echo($insauthkey);?>",domain:"<?php echo($domainurl);?>",interface:"<?php echo($interface);?>",sitesoftwaretype:"uchome",sitesoftwareversion:"<?php echo(X_VER);?>",isfulltoolbar:"1",build:"<?php echo($pluginCfg['release']);?>",isthread:"1",version:"<?php echo($pluginCfg['version'][X_VER]);?>"};
         createScript(serverurl,activityparm,function(){
             if(typeof(result)=="undefined"){
                  window.location.href='<?php echo(SELF_FILE);?>?action=insfinish&sid=1&result=9999';
             }else{                 
                  window.location.href="<?php echo(SELF_FILE);?>?action=insfinish&sid=1&result="+result;
             }
         });
    }      
</script>
<?php
   imshow_footer();
   
   function imshow_header(){
        print <<<EOF
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Ntalker插件安装包 For UCHome</title>
        <link rel="stylesheet" media="screen" href="css/style_index.css" />
        <link rel="stylesheet" media="screen" href="css/base.css" />
        <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
        </head>
        <body>
        <div  id="container">
        <!--头部-->
        <div class="top">
          <div class="logo"></div><div class="top_right">技术支持：010-64419266-8011<br />Ntalker V3.1 For UCHome版</div>
        </div>
         <script language="javascript">
            if(document.getElementById('msgDiv'))
            {
                document.getElementById('msgDiv').scrollTop=document.getElementById('msgDiv').scrollHeight;
            }
         </script>
         <script language="javascript" type="text/javascript">   
            function $\$O(id){
                return document.getElementById(id)||null;
            };
            function createScript(url, jsonpram, callback){
                var src, strPram = [];
                switch( typeof(jsonpram) ){
                       case 'undefined': return;break;
                       case 'string': 
                           src = url + '?' + jsonpram;
                           break;
                       case 'object':
                           for(var k in jsonpram){
                              if( typeof(jsonpram[k]) == 'string' ){
                                     strPram.push(k + '=' + jsonpram[k]);
                                  }
                           }
                           src = url + '?' + strPram.join('&');
                          break;
                }
                var head = document.getElementsByTagName("HEAD")[0], 
                    elem = document.createElement("SCRIPT"), 
                    done = false;
                elem.setAttribute('language', 'javascript');
                elem.setAttribute('type', 'text/javascript');
                elem.setAttribute('charset', 'utf-8');
                elem.setAttribute('src',src);
                elem.onload = elem.onreadystatechange = elem.onerror = function(){
                    if ( !done && ( !this.readyState || /loaded|complete/.test(this.readyState) ) ) {
                        done = true;
                        if( typeof(callback) == "function" ) callback();
                    }
                };
                head.appendChild(elem);
                return elem;
            }
         </script>
EOF;
   }
   function imshow_footer($quit=true){
        print <<<EOF
        <div class="bot">能通天下版权所有 京ICP证:080695号 Copyright 2010 Ntalker.com. All Rights Reserved</div>
        </div>
        </body>
        </html>
EOF;
   $quit && exit();
   }
   function interact($jsonparms,$callback=''){
        global $recordurl;
        echo '<script language="javascript" type="text/javascript">';
        echo 'createScript("'.$recordurl.'",';
        echo $jsonparms;
        if($callback){
             echo','.$callback.');';
        }else{
             echo ');';
        }
        echo '</script><br />';
   }
   function imshow_msg($errtitle,$addmsg='',$dealmsg,$iscontinue=false,$isrecheck=false){
        global $pluginCfg;
        echo '<div class="topnav" style="text-align:left;">错 误 提 示</div>
              <div class="cont" style="height:400px;">';   
        echo '<p class="f14 f12_orange f_blod">'.$errtitle.'</p>';
        if($addmsg){
             echo '<p>'.$addmsg.'</p>';
        }
        echo '<p class="f14 f_blod">解决办法</p>';
        echo '<p>'.$dealmsg.'</p>';
        echo '<p>问题仍无法解决？联系Ntalker技术支持（邮箱：<a href="mailto:ntalker@ntalker.com"><span class="f_blue">ntalker@ntalker.com</span></a>，电话：010-64419266-8011）</p>';
        if($iscontinue){
             echo '</div><div class="bu2">';
        }else{
             echo '</div><div class="bu">';
        }
        if($iscontinue){
             echo '<INPUT type="button" class="but_" value="继续安装" onclick="window.location.href=\''.SELF_FILE.'?sid=1&newversion='.$pluginCfg['version'][X_VER].'\';" />';
        }
        if($isrecheck){
             echo '<INPUT type="button" class="but_" value="重新检查" onclick="window.location.href=\''.SELF_FILE.'?sid=1\';" />';
        }else{
             echo '<INPUT TYPE="button" class="but_" value="退  出" onclick="window.location.href=\''.INSSITEURL.'\';" />';
        }
        echo '</div>';
        imshow_footer();
   }
?>